package com.example.todolist.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {

	public static boolean isNullOrEmpty(String path) {
		if (path == null || path.length() == 0) {
			return true;
		} else {
			return false;
		}
	}

	public static String translateNull2Empty(String data) {
		if (data == null) {
			return "";
		} else {
			return data;
		}
	}

	public static String removeSymbol(String data) {
		if (data.endsWith("?") || data.endsWith("!")) {
			return data.substring(0, data.length() - 1);
		} else {
			return data;
		}
	}

	public static String getSha1(String input) {
		MessageDigest mDigest = null;
		byte[] result = null;

		try {
			mDigest = MessageDigest.getInstance("SHA1");
			result = mDigest.digest(input.getBytes());

		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}

		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < result.length; i++) {
			sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
		}
		return sb.toString();
	}
	
	public static boolean isOnlyBlank(String str) {  
        String dest = "";  
        if (str != null) {
                Pattern p = Pattern.compile("\\s*|\t|\r|\n");  
                Matcher m = p.matcher(str);  
                dest = m.replaceAll("");
        } 
        if("".equals(dest)) {
        	return true;
        }
        return false;  
}  

}
